package cn.tedu.shoot;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class World extends JPanel {
	public static final int WIDTH = 480;
	public static final int HEIGHT = 853;
	public static final int START = 0;
	public static final int RUNNING = 1;
	public static final int PAUSE = 2;
	public static final int GAME_OVER = 3;
	public static final int GAME_INSIDE = 4;
	public static final int CHOOS = 5;
	public static int state = START ;
	public boolean start = false;
	public boolean start1 = false;
	public boolean start2 = false;
	public boolean start3 = false;
	public static int score = 0;
	public static int num = 0;
	public static int heroNum = 0;
	private static final int TIMEEND = 2000;
	private Sky sky = new Sky();
	private Hero hero = new Hero(heroNum);
	private List<FlyingObject> enemies = new ArrayList<>();
	private List<Bullet> bullets = new ArrayList<>();
	private List<FlyingObject> bossbullets = new ArrayList<>();
	private List<BulletBoss> bulletsboss = new ArrayList<>();
	private ExecutorService threadPool = Executors.newFixedThreadPool(50);
	/** 生成随机敌人 */
	int time = 0;
	int sum = 0;

	public FlyingObject nextOne() {
		time++;
		if (time >= TIMEEND - 200 && sum == 0) {// BOSS出来提前两秒有声音
			sum = 1;
			playMusic("warning.mp3", false);
		}
		if (time <= TIMEEND) {
			Random rand = new Random();
			int type = rand.nextInt(20);
			if (type < 1) {
				return new Bee();
			} else if (type < 12) {
				return new Airplane();
			} else {
				return new BigAirplane();
			}
		} else {
			return new BossHero();
		}
	}

	/** 扩容敌人个数 */
	int x = 0;
	int enterIndex = 0;

	public void enterAction() {

		enterIndex++;
		if (enterIndex % 30 == 0 && x == 0) {
			enemies.add(nextOne());
			FlyingObject boss = enemies.get(enemies.size() - 1);
			if (boss instanceof BossHero) {
				x = 1;// 生成的敌人是Boss
			}
		}
	}

	/** 扩容子弹个数 */
	int shootIndex = 0;

	public void shootAction() {
		shootIndex++;
		synchronized (hero) {
			if (shootIndex % 30 == 0 && (hero.isLife() || hero.isBigR())) {
				bullets.addAll(hero.shoot(hero.getDoubleFire()));
			}
		}
	}

	/** 敌人发射子弹 */
	int shotsIndex = 0;

	public void shootingShotsAction() {
		shotsIndex++;
		for (FlyingObject e : enemies) {
			if (e instanceof EnemyBullets) {
				if (e instanceof BossHero) {
					if (shotsIndex % 60 == 0 && e.isLife()) {
						// bossbullets.addAll(e.shootingShots());
						BossHero boss = (BossHero) e;
						bulletsboss.addAll(boss.shootingShoot());
					}
				} else if (shotsIndex % 100 == 0 && e.isLife()) {
					bossbullets.addAll(e.shootingShots());
				}
			}
		}
	}

	/** 敌人(BOSS)发射的子弹攻击英雄机 */
	public void shootHeroAction() {
		for (FlyingObject b : bossbullets) {
			if (b.isLife() && hero.hit(b)) {
				if (hero.isLife()) {
					b.state = b.DEAD;
					hero.cutlife(hero);
					hero.cleanDoubleFire();
				}
				if (hero.isBigR()) {
					b.state = b.DEAD;
				}
			}
		}
		for (BulletBoss b : bulletsboss) {
			if (b.isLife() && hero.hit(b)) {
				if (hero.isLife()) {
					b.state = b.DEAD;
					hero.cutlife(hero);
					hero.cleanDoubleFire();
				}
				if (hero.isBigR()) {
					b.state = b.DEAD;
				}
			}
		}

	}

	/** 英雄机移动 */
	int time2 = 0;

	public void stepHero() {
		if (start2 && time2++ <= 80) {
			hero.step();
		}
	}

	/** 飞行物,天空,子弹移动 */
	public void stepAction() {
		sky.step();
		for (FlyingObject enemy : enemies) {
			enemy.step();
		}
		for (Bullet b : bullets) {
			b.step();
		}
		for (BulletBoss b : bulletsboss) {
			b.step();
		}
		for (FlyingObject bossbullet : bossbullets) {
			bossbullet.step();
		}
	}

	/** 删除越界敌人，子弹 */
	public synchronized void outOfBoundsAction() {
		// Collection<FlyingObject> f = new ArrayList<>();
		// for (FlyingObject enemy : enemies) {
		// if (enemy.outOfBounds() || enemy.isRemove()) {
		// f.add(enemy);
		// }
		// }
		// enemies.removeAll(f);
		Iterator<FlyingObject> f = enemies.iterator();
		while (f.hasNext()) {
			FlyingObject b = f.next();
			if (b.outOfBounds() || b.isRemove()) {
				f.remove();
			}
		}

		Iterator<Bullet> it = bullets.iterator();
		while (it.hasNext()) {
			Bullet b = it.next();
			if (b.outOfBounds() || b.isRemove()) {
				it.remove();
			}
		}
		Iterator<FlyingObject> b = bossbullets.iterator();
		while (b.hasNext()) {
			FlyingObject bs = b.next();
			if (bs.outOfBounds() || bs.isRemove()) {
				b.remove();
			}
		}
		Iterator<BulletBoss> boss = bulletsboss.iterator();
		while (boss.hasNext()) {
			BulletBoss bs = boss.next();
			if (bs.outOfBounds() || bs.isRemove()) {
				boss.remove();
			}
		}
	}

	/** 子弹与子弹碰撞 */
	public void bulletAndBullet() {
		// List<Bullet> b = new ArrayList<>();
		// List<BossBullet> boss = new ArrayList<>();
		for (Bullet bullet : bullets) {
			for (FlyingObject bossbullet : bossbullets) {
				if (bullet.hit(bossbullet)) {
					bullet.goDead();
					bossbullet.goDead();
				}
			}
			for (BulletBoss bb : bulletsboss) {
				if (bullet.hit(bb)) {
					bullet.goDead();
					bb.goDead();
				}
			}
		}
	}

	/** 子弹与敌人撞击 */

	public void bulletBangAction() {
		for (Bullet b : bullets) {
			for (FlyingObject e : enemies) {
				if (b.isLife() && e.isLife() && e.hit(b)) {// 判断是否活着，是否相撞
					b.goDead();
					if (e instanceof BossHero) { // 判断是否为BOSS类型
						e.cutlife(e);/* BOSS减命 */
						if (e.getLife(e) <= 0) { // 如果没命了goDead
							e.goDead();
							score += ((BossHero) e).getScore();
							hero.addDoubleFire(250);// 如果为BOSS则加150火力
							hero.addLife(3);// 如果是BOSS则加3条命
							playMusic("bossdead.mp3", false);
						}
					} else {// 不是BOSS类型
						e.goDead();
						playMusic("bullet.mp3", false);
						if (e instanceof Enemy) { // 判断是否为得分类型
							Enemy f = (Enemy) e;
							score += f.getScore();
						}
						if (e instanceof Award) { // 判断是否为奖励类型
							Award f = (Award) e;
							int type = f.getAwardType();
							switch (type) {
							case Award.DOUBLE_FIRE:
								hero.addDoubleFire(150);// 如果参数为英雄则加100火力
								break;
							case Award.LIFE:
								hero.addLife(1);// 如果小蜜蜂命加1
								break;
							}
						}
					}
				}
			}
		}
	}

	/** 英雄机与敌人相撞 */
	public void heroBangAction() {
		for (FlyingObject e : enemies) {
			if (e.isLife() && e.hit(hero)) {
				if (hero.isLife()) {
					if (e instanceof BossHero) {
						BossHero boss = (BossHero) e;
						boss.cutlife(boss);
						if (boss.getLife(boss) <= 0) {// BOSS生命是否为0
							boss.goDead();
							playMusic("bossdead.mp3", false);
						}
					} else {
						e.goDead();
					}
					hero.cutlife(hero);
					hero.cleanDoubleFire();
				}
				if (hero.isBigR()) {
					e.goDead();
				}
			}
		}
	}

	/** 杀死BOSS后 */
	public void killBoss() {
		if (x == 1 && enemies.size() == 0) {
			time = 0;
			x = 0;
			sum = 0;
		}
	}

	/** 英雄机死亡 */
	boolean m = true;

	public void checkGameOverAction() {
		if (hero.getLife(hero) <= 0 && m) {// 判断英雄机的命是否为0
			hero.goDead();
			WriteScore.write();// 将分数写入score.txt
			playMusic("bossdead.mp3", false);
			m = false;
		}
	}

	/** 游戏结束状态 */
	public void deadAction() {
		if (hero.isRemove()) {
			state = GAME_OVER;
			if (state == GAME_OVER) {
				score = 0;
				hero = new Hero(heroNum);
				enemies.clear();
				bullets.clear();
				bossbullets.clear();
				bulletsboss.clear();
				m = true;
				time = 0;
				sum = 0;
				x = 0;
			}
			// System.out.println("mmm");
		}
	}

	/** 放大招 */
	public void bigR() {
		if (state == GAME_INSIDE) {
			Iterator<FlyingObject> it = enemies.iterator();
			while (it.hasNext()) {
				FlyingObject f = it.next();
				if (f instanceof BossHero) {
					BossHero bh = (BossHero) f;
					if (bh.life > 20) {
						bh.life -= 20;
						Iterator<FlyingObject> bs = bossbullets.iterator();
						while (bs.hasNext()) {
							FlyingObject b = bs.next();
							b.goDead();
						}
						Iterator<Bullet> bb = bullets.iterator();
						while (bb.hasNext()) {
							Bullet b = bb.next();
							b.goDead();
						}
						Iterator<BulletBoss> boss = bulletsboss.iterator();
						while (boss.hasNext()) {
							BulletBoss b = boss.next();
							b.goDead();
						}
					} else {
						hero.judge += 1;
					}
				} else {
					f.goDead();
					playMusic("bigr.mp3", false);
					if (f instanceof Award) {
						Award e = (Award) f;
						int type = e.getAwardType();
						switch (type) {
						case Award.DOUBLE_FIRE:
							hero.addDoubleFire(50);// 如果参数为英雄则加50火力
							break;
						case Award.LIFE:
							hero.addLife(1);// 如果小蜜蜂命加1
							break;
						}
					}
					if (f instanceof Enemy) {
						score += ((Enemy) f).getScore();
					}
					if (f instanceof EnemyBullets) {
						Iterator<FlyingObject> bs = bossbullets.iterator();
						while (bs.hasNext()) {
							FlyingObject b = bs.next();
							b.goDead();
						}
					}
				}
			}
			state = RUNNING;
		}
	}

	/** 判断是否为开始状态 */
	public boolean startZt() {
		if (state == START) {
			n2 = 0;
			return true;
		} else {
			return false;
		}
	}

	/** 判断是否为选择状态 */
	public boolean choosZt() {
		if (state == CHOOS) {
			return true;
		}
		return false;
	}

	/** 判断是否为运行状态 */
	public boolean runningZt() {
		if (state == RUNNING) {
			return true;
		} else {
			return false;
		}
	}

	/** 判断是否为暂停状态 */
	public boolean pauseZt() {
		if (state == PAUSE) {
			return true;
		} else {
			return false;
		}
	}

	int n2 = 0;

	/** 判断是否为结束状态 */
	public boolean gameoverZt() {
		if (state == GAME_OVER) {
			if (n2 == 0) {
				n2 = 1;
				playMusic("gameovermusic.mp3", false);
			}
			return true;
		} else {
			return false;
		}
	}

	/** 判断Boss是否为死亡 */
	public synchronized boolean bossIsDead() {
		if (x == 1) {
			for (FlyingObject b : enemies) {
				if (b instanceof BossHero) {
					if (b.isLife() || b.isDead()) {
						return false;
					} else {
						return true;
					}
				}
			}
		}
		return false;

	}

	/** 判断英雄机是否为护盾模式 */
	public void shield() {
		if (hero.isBigR() && num == 1) {
			num = 0;
			playMusic("shield.mp3", false);
		}
	}

	/** 背景音乐 */
	int n = 0;
	boolean result = true;

	public void backMusic(int num) {
		// System.out.println(n+","+num);
		Thread t1 = new Thread() {
			public void run() {
				while (n == num) {
					if (startZt() || choosZt()) {
						Music music = new Music(getClass().getResourceAsStream("/music/start1.mp3"));
						music.run();
					}
				}
			}
		};
		t1.start();
		Thread t2 = new Thread() {
			public void run() {
				while (num == n) {
					if (runningZt()) {
						t1.stop();
						Music music = new Music(getClass().getResourceAsStream("/music/backmusic.mp3"));
						music.run();
					}
				}
			}
		};
		t2.start();
		Thread t4 = new Thread() {
			public void run() {
				while (num == n) {
					if (time >= TIMEEND && !pauseZt()) {
						t2.stop();
						Music music = new Music(getClass().getResourceAsStream("/music/bossmusic.mp3"));
						music.run();
					}
				}
			}
		};
		t4.start();
		Thread t3 = new Thread() {
			public void run() {
				while (num == n) {
					if ((bossIsDead() || gameoverZt() || pauseZt()) && result) {
						result = false;
						t4.stop();
						t2.stop();
						n++;
						int m = num + 1;
						backMusic(m);// 音乐暂停
					}
					if (runningZt()) {
						result = true;
					}
					if (bossIsDead()) {
						time = 0;
						t4.stop();
					}
				}
			}
		};
		t3.start();

	}

	/** 音乐 */
	public void playMusic(String name, boolean result) {
		String ename = "/music/"+name;
		Runnable r1 = () -> {
			try {
				if (result) {
					while (true) {
						Music music = new Music(getClass().getResourceAsStream(ename));
						music.run();
					}
				} else {
					Music music = new Music(getClass().getResourceAsStream(ename));
					music.run();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		};
		threadPool.execute(r1);
	}

	/** 选择英雄机型 */
	int b = 0;

	public void choos() {
		if (state == CHOOS && b == 1) {
			// hero.state = Hero.REMOVE;
			hero = new Hero(heroNum);
			// System.out.println(heroNum+"号机");
			b = 0;
		}
	}

	/** 启动程序执行 */
	public void action() {
		backMusic(0);
		Timer timer = new Timer();
		int intervel = 10;
		timer.schedule(new TimerTask() {// 匿名内部类
			public void run() {
				if (state == RUNNING) {
					if (time < TIMEEND + 200) {// BOSS机报警音乐4秒
						nextOne();// 生成随机敌人
					}
					enterAction();// 遍历所有敌人
					shootAction();// 遍历所有子弹
					stepAction();// 所有飞行物移动
					bulletBangAction();// 子弹碰撞敌人
					heroBangAction();// 英雄碰撞敌人
					shootingShotsAction();// BOSS发射子弹
					shootHeroAction();// BOSS发射的子弹与英雄碰撞
					outOfBoundsAction();// 越过界限删除
					bulletAndBullet();// 子弹与子弹碰撞
					killBoss();// 杀死BOSS后
					deadAction();// 英雄机被删除改变状态
					checkGameOverAction();// 英雄机死亡
					hero.move();// 键盘控制移动
					shield();// 判断是否有开启护盾模式
				}
				choos();
				stepHero();
				bigR();// 大招
				repaint();// 重画（重新调用paint方法)
			}
		}, intervel, intervel);

		// 事件触发鼠标移动得坐标
		MouseAdapter l = new MouseAdapter() {
			public void mouseMoved(MouseEvent e) {// 鼠标移动
				// if (state == RUNNING && hero.isLife()) {//鼠标控制游戏移动
				// int x = e.getX();
				// int y = e.getY();
				// hero.moveTo(x, y);
				// }
				if (state == START || state == PAUSE) {// 开局控制开始按钮位置
					if (e.getX() > 150 && e.getX() < 310 && e.getY() > 700 && e.getY() < 770) {
						start = true;// 鼠标在开始游戏界面就为true
					} else {
						start = false;// 鼠标不在开始游戏界面就false
					}
				}
				if (state == START) {// 选择英雄按钮
					if (e.getX() > 132 && e.getX() < 345 && e.getY() > 530 && e.getY() < 623) {
						start1 = true;
					} else {
						start1 = false;
					}
				}
				if (state == CHOOS) {
					if (e.getX() > 156 && e.getX() < 324 && e.getY() > 700 && e.getY() < 734) {// 确定英雄按钮
						start3 = true;
					} else {
						start3 = false;
					}
				}
			}

			public void mouseClicked(MouseEvent e) {// 鼠标点击
				switch (state) {
				case START:
					if (e.getX() > 150 && e.getX() < 310 && e.getY() > 700 && e.getY() < 770) {// 游戏开始按钮
						hero.x = WIDTH / 2 - hero.width / 2;
						hero.y = 600;
						state = RUNNING;
						playMusic("click.mp3", false);
						playMusic("start.mp3", false);
						start = false;
					}
					if (e.getX() > 132 && e.getX() < 345// 选择英雄按钮的位置
							&& e.getY() > 530 && e.getY() < 623) {
						hero.x = WIDTH / 2 - hero.width / 2;
						hero.y = 465;
						hero.state = Hero.REMOVE;
						state = CHOOS;
					}
					break;
				case RUNNING:
					state = PAUSE;
					break;
				case PAUSE:
					if (e.getX() > 150 && e.getX() < 310 && e.getY() > 700 && e.getY() < 770) {// 暂停开始按钮
						state = RUNNING;
						playMusic("click.mp3", false);
						start = false;
					}
					break;
				case GAME_OVER:
					score = 0;
					enemies.clear();
					bullets.clear();
					m = true;
					time = 0;
					x = 0;
					state = START;
					break;
				case CHOOS:
					if (e.getX() > 156 && e.getX() < 324 && e.getY() > 700 && e.getY() < 734) {// 确定英雄按钮的位置
						start2 = false;
						time2 = 0;
						hero.x = World.WIDTH / 2 - hero.width / 2;
						if (heroNum != 2) {// 设置start状态的飞机位置
							hero.y = 325 - hero.height / 2;
						} else {
							hero.y = 315;
						}
						if (!start2) {// 如果没选则默认选择原先的
							hero = new Hero(heroNum);
						}
						state = START;
					}
					if (e.getX() > WIDTH / 2 - hero.width / 2 && e.getX() < WIDTH / 2 + hero.width / 2 && e.getY() > 465
							&& e.getY() < 465 + hero.height) {// 选择0号机型
						playMusic("push1.mp3", false);
						time2 = 0;
						heroNum = 0;
						b = 1;
						start2 = true;
					}
					if (e.getX() > 43 && e.getX() < 43 + 115 && e.getY() > 285 && e.getY() < 285 + 100) {// 选择1号机型
						playMusic("push3.mp3", false);
						time2 = 0;
						heroNum = 1;
						b = 1;
						start2 = true;
					}
					if (e.getX() > 342 && e.getX() < 342 + 83 && e.getY() > 285 && e.getY() < 285 + 83) {// 选择2号机型
						playMusic("push2.mp3", false);
						time2 = 0;
						heroNum = 2;
						b = 1;
						start2 = true;
					}
					break;
				}
			}

			public void mouseExited(MouseEvent e) {// 鼠标移出
				// if (state == RUNNING) {
				// state = PAUSE;
				// }
			}

			public void mouseEntered(MouseEvent e) {// 鼠标移入
				// if (state == PAUSE) {
				// state = RUNNING;
				// }
			}
		};
		this.addMouseListener(l);// 侦听器
		this.addMouseMotionListener(l);

	}

	/** 画笔 */
	int i = 0;
	int y = 0;

	public void paint(Graphics g) {

		sky.paintObject(g);
		if ((time >= TIMEEND - 220 && time <= TIMEEND - 170)// 跟报警音乐配合时机
				|| (time >= TIMEEND - 120 && time <= TIMEEND - 70) || (time >= TIMEEND - 20 && time <= TIMEEND + 30)
				|| (time >= TIMEEND + 80 && time <= TIMEEND + 130)) {// BOSS出来两秒前显示警告图片
			g.drawImage(Images.warning, 123, 100, null);
		}
		switch (state) {
		case START:
			g.drawImage(Images.start[0], 0, 0, null);
			g.drawImage(Images.start[3], 170, 50, null);
			g.drawImage(Images.start[1], 160, 725, null);
			g.drawImage(Images.start[4], 132, 530, null);
			if (start1) {
				g.drawImage(Images.start[5], 132, 530, null);
			}
			if (start) {
				g.drawImage(Images.start[2], 132, 700, null);
			}
			break;
		case PAUSE:
			g.drawImage(Images.start[0], 0, 0, null);
			g.drawImage(Images.start[3], 170, 50, null);
			g.drawImage(Images.pause[0], 110, 200, null);
			g.drawImage(Images.pause[1], 133, 705, null);
			if (start) {// 判断鼠标是否在范围内
				g.drawImage(Images.pause[2], 133, 705, null);
			}
			break;
		case GAME_OVER:
			g.drawImage(Images.gameover, 0, 0, null);
			if (y == 0) {
				y = 1;
				int y = 133;
				Graphics p = Images.getGraphics();// 获取gameover的画笔
				p.setFont(new Font("雅黑", Font.BOLD, 24));
				p.setColor(new Color(15, 250, 255));
				for (int i = 0; i < 10; i++) {
					p.drawString(i + 1 + "", 45, y + i * 58);
				}
				p.setFont(new Font("雅黑", Font.BOLD, 14));
				List<String> list = WriteScore.read();
				int i = 1;
				int x = 75;
				for (String str : list) {
					p.drawString(str, x, y);
					 System.out.println(str+" ");
					if (i == 1) {
						x += 45;
					}
					x += 45;
					if (i++ % 3 == 0) {
						y += 58;
						x = 75;
						i = 1;
						 System.out.println();
					}
				}
			}
			break;
		case CHOOS:
			g.drawImage(Images.choos[0], 0, 0, null);
			g.drawImage(Images.choos[1], 156, 700, null);
			g.drawImage(Images.hero[0], 208, 470, null);
			g.drawImage(Images.hero1, 43, 285, null);
			g.drawImage(Images.hero2, 342, 305, null);
			g.drawImage(Images.choos[4], 177, 100, null);
			if (start3) {
				g.drawImage(Images.choos[2], 156, 700, null);
			}
			if (start2 && heroNum == 0) {
				g.drawImage(Images.choos[3], 178, 400, null);
			}
			if (start2 && heroNum == 1) {
				g.drawImage(Images.choos[3], 38, 253, null);
			}
			if (start2 && heroNum == 2) {
				g.drawImage(Images.choos[3], 320, 253, null);
			}
			break;
		}
		if (state != GAME_OVER && state != PAUSE) {
			hero.paintObject(g);
		}
		if (state == RUNNING) {
			for (FlyingObject e : enemies) {
				e.paintObject(g);
				if (e instanceof BossHero) {
					BossHero boss = (BossHero) e;
					boss.addBlood().paintObject(g);
				}
			}
			for (Bullet b : bullets) {
				b.paintObject(g);
			}
			for (BulletBoss b : bulletsboss) {
				b.paintObject(g);
			}
			for (FlyingObject bossbullet : bossbullets) {
				bossbullet.paintObject(g);
			}

			g.setColor(Color.WHITE);
			g.drawString("SCORE:" + score, 10, 25);
			g.drawString("LIFE:" + hero.getLife(hero), 10, 45);
			g.drawString("防护罩:" + (5 - hero.times2), 10, 65);
			g.drawString("大招:" + (hero.judge - hero.times1), 10, 85);
		}

	}

	public static void main(String[] args) {
		JFrame frame = new JFrame(); // 创建窗口
		World world = new World(); // 创建面板
		frame.add(world); // 将面板添加到窗口中
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭窗口时结束程序
		frame.setSize(WIDTH, HEIGHT); // 设置窗口大小
		frame.setTitle("朱雄峰的游戏");
		frame.setLocationRelativeTo(null); // 设置居中显示
		frame.setVisible(true); // 1)设置窗口可见 2)尽快调用paint()方法
		frame.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {

			}

			@Override
			public void keyReleased(KeyEvent e) {
				world.hero.minusDirection(e);
			}

			@Override
			public void keyPressed(KeyEvent e) {
				world.hero.addDirection(e);
			}
		});
		world.action(); // 启动程序的执行
	}

}
